﻿using Common.Library;
using PicasaMobileInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using System.Xml.Linq;

namespace Social_Celebrities
{
  public  class PostToblog
    {

      public AutoResetEvent auto = new AutoResetEvent(false);
    
      public string Comment = string.Empty;
      
       public void Login(string username,string password)
       {
           BloggerInterface BI = new BloggerInterface(username, password);
       }

      public void UploadItem(string comment,AutoResetEvent Auto,string BlogID,string PostID)
       {
           try
           {
               auto = Auto;
               Comment = comment;
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.blogger.com/feeds/" + BlogID + "/" + PostID + "/comments/default");
               request.Method = "POST";
               request.ContentType = "application/atom+xml";
               request.Headers["Authorization"] = CONST.PIC_AUTH + AppSettings.BloggerAccessToken;
               request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallbackItem), request);
           }
           catch
           {
               Constants.CommentPosted = false;
               auto.Set();
           }
      }
      private void GetRequestStreamCallbackItem(IAsyncResult asynchronousResult)
      {
          try
          {
              HttpWebRequest webrequest = (HttpWebRequest)asynchronousResult.AsyncState;
              BlogData(asynchronousResult, webrequest);
          }
          catch
          {
              Constants.CommentPosted = false;
              auto.Set();
          }
      }

     
        private void BlogData(IAsyncResult asynchronousResult, HttpWebRequest request,string datacustom="")
        {
           try
           {
            Stream postStream = new MemoryStream();
            string contents = "<entry xmlns='http://www.w3.org/2005/Atom'>" +
                              "<author><name>"+AppSettings.BlogUserName+"</name></author>"+
                              "<content type='text'>"+Comment+"" +
                              "</content>" +
                              "</entry>";
           
            Stream ms = new MemoryStream(Encoding.UTF8.GetBytes(contents));
            postStream = request.EndGetRequestStream(asynchronousResult);
            byte[] buffer = new byte[ms.Length / 4];
            int bytesRead = -1;
            ms.Position = 0;
            while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) > 0)
            {
                postStream.Write(buffer, 0, bytesRead);
            }
            ms.Flush();
            ms.Close();
            postStream.Flush();
            postStream.Close();
          
            request.BeginGetResponse(new AsyncCallback(RequestAlbumCompletedItem), request);
           }
           catch
            {
                Constants.CommentPosted = false;
                auto.Set();
            }
        }

        private void RequestAlbumCompletedItem(IAsyncResult result)
        {
            try
            {
                var request = (HttpWebRequest)result.AsyncState;
                var response = (HttpWebResponse)request.EndGetResponse(result);
                StreamReader responseReader = new StreamReader(response.GetResponseStream());
                string responseStr = responseReader.ReadToEnd(); 
                Constants.CommentPosted = true;
                auto.Set();
            }
            catch
            {
                Constants.CommentPosted = false;
                auto.Set();
            }
        }
    }
}
